import ruleStore from "@/stores/commonAsyncOptions";
import { getEsIndexsApi, getWebsiteGroupApi, getImgAndFontAndLogoApi, getWebsiteTypeApi } from "@/api/esArticle";
import { message } from "ant-design-vue";
import { useArrayMap, computedAsync } from "@vueuse/core";
import { cloneDeep } from "@/utils/lodash";
// 获取一个表格最小高度和{x,y}
export function useScrollxy() {
    const minHeight = ref(window.innerHeight - 200);
    const scrollComp = computed(() => {
        return { x: undefined, y: minHeight.value };
    });
    return {
        minHeight,
        scrollComp
    };
}

export function useCommonColumn() {
    return [
        {
            title: "下载数量",
            key: "dlownnum",
            dataIndex: "dlownnum"
        },
        {
            title: "发布数量",
            key: "pubnum",
            dataIndex: "pubnum"
        },
        {
            title: "beta数",
            key: "beta",
            dataIndex: "beta"
        },
        {
            title: "小狗数",
            key: "xg",
            dataIndex: "xg"
        }
    ];
}
// type 1 === input + select
// type 2 === input
// type 3 === time
type OtherSearchItem = {
    title: string;
    value: string;
    key: string;
    type: 1 | 2 | 3;
    selectValue?: string;
};
export function useCommonAsyncOptions(type: "get" | "post" = "post", isMounted: boolean = true) {
    const imgDirOptions = ref<SelectOption>([]);
    const fontOptions = ref<SelectOption>([]);
    const logoDirOptions = ref<SelectOption>([]);

    const getImgAndFontAndLogo = () => {
        getImgAndFontAndLogoApi().then((res: any) => {
            if (res.status === 200) {
                const { img_dirs, font_list, logo_dirs } = res.data;
                // 通过useArrayMap遍历Ref数组,处理每个数组的值
                const arrComputes = useArrayMap([img_dirs, font_list, logo_dirs], (item: Array<string>) =>
                    item.map((v: string) => ({ label: v, value: v }))
                );
                imgDirOptions.value = arrComputes.value[0];
                fontOptions.value = arrComputes.value[1];
                logoDirOptions.value = arrComputes.value[2];
            }
        });
    };
    // 发布类型
    const publishTypeOptions = ref<RuleStoreStateOptions["publish_type_name"]>([]);
    // 规则类型
    const ruleOptions = ref<SelectOption>([]);
    // 用户列表
    const userOptions = ref<SelectOption>([]);
    // 伪原创
    const artWycOptions = ref<SelectOption>([]);
    // 自定义标识符
    const customIdentifierOptions = ref<SelectOption>([]);
    const $store = ruleStore();
    const getPublishTypeAndUserListAndRuleType = () => {
        $store.init("all").then((obj) => {
            const { custom_identifier_name, wyc_name, rule_type_name, publish_type_name, create_name } = obj.value;
            // artWycOptions(index = 1)和userOptions(index = 2) index = 3已存在默认值
            userOptions.value = create_name;
            artWycOptions.value = wyc_name;
            // @ts-ignore
            publishTypeOptions.value = addDefaultItem(cloneDeep(publish_type_name));
            ruleOptions.value = addDefaultItem(cloneDeep(rule_type_name));
            customIdentifierOptions.value = addDefaultItem(cloneDeep(custom_identifier_name));
        });
    };
    const websiteGroupOptions = computedAsync<SelectOption>(getWebsiteGroupOptions, []);
    // 获取网站组列表
    function getWebsiteGroupOptions() {
        return new Promise<SelectOption>((resolve) => {
            getWebsiteGroupApi()
                .then((res) => {
                    if (res.status === 200) {
                        if (type === "get") {
                            res.data.data.unshift({ label: "请选择", value: 0 });
                        }
                        resolve(res.data.data);
                    }
                })
                .catch(() => message.error("服务器错误!"));
        });
    }
    const websiteTypeOptions = computedAsync<SelectOption>(getWebsiteType, []);
    // 获取网站类型列表
    function getWebsiteType() {
        return new Promise<SelectOption>((resolve) => {
            getWebsiteTypeApi()
                .then((res) => {
                    if (res.status === 200) {
                        if (type === "get") {
                            res.data.data.unshift({ label: "请选择", value: 0 });
                        }
                        resolve(res.data.data);
                    }
                })
                .catch(() => message.error("服务器错误!"));
        });
    }

    onMounted(() => {
        if (isMounted) {
            getImgAndFontAndLogo();
            getPublishTypeAndUserListAndRuleType();
        }
    });
    // 给Options 添加一项默认值
    function addDefaultItem(options: SelectOption) {
        if (type === "get") {
            const defaultItem = { label: "请选择", value: "" };
            options.unshift(defaultItem);
        }
        return options;
    }
    return {
        artWycOptions,
        ruleOptions,
        customIdentifierOptions,
        userOptions,
        imgDirOptions,
        fontOptions,
        logoDirOptions,
        publishTypeOptions,
        addDefaultItem,
        websiteGroupOptions,
        websiteTypeOptions
    };
}
export function useCommonStaticOptions() {
    // ES服务器
    const esHostOptions: SelectOption = [
        {
            label: "192.168.1.90",
            value: "http://192.168.1.90:9200"
        },
        {
            label: "192.168.1.91",
            value: "http://192.168.1.91:9200"
        },
        {
            label: "192.168.1.92",
            value: "http://192.168.1.92:9200"
        },
        {
            label: "192.168.1.94",
            value: "http://192.168.1.94:9200"
        }
    ];
    const weekOptions: SelectOption = [
        {
            label: "周一",
            value: 1
        },
        {
            label: "周二",
            value: 2
        },
        {
            label: "周三",
            value: 3
        },
        {
            label: "周四",
            value: 4
        },
        {
            label: "周五",
            value: 5
        },
        {
            label: "周六",
            value: 6
        },
        {
            label: "周日",
            value: 0
        },

    ];
    // 过滤的filter
    const filterOption = (input: string, option: any) => {
        return option.label.indexOf(input) >= 0;
    };
    // 其他搜索Options
    const otherSearchOptions: Array<OtherSearchItem> = [
        {
            title: "正文",
            key: "content",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "域名",
            key: "domain",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "内容分类",
            key: "content_type",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "内容分类2",
            key: "content_type2",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "内容分类3",
            key: "content_type3",
            value: "",
            type: 1,
            selectValue: ""
        },

        {
            title: "是否为关键词采集",
            key: "is_key_search",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "运营负责人名称",
            key: "person_in_charge",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "搜索词",
            key: "search_key",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "seo描述",
            key: "seo_description",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "seo关键词",
            key: "seo_keywords",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "文章tag",
            key: "tag",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "文章标题",
            key: "title",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "当前栏目url",
            key: "typeurl",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "当前文章url",
            key: "url",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "标题",
            key: "seo_title",
            value: "",
            type: 1,
            selectValue: ""
        },
        {
            title: "下载次数",
            key: "dlownnum",
            value: "",
            type: 3
        },
        {
            title: "发布次数",
            key: "pubnum",
            value: "",
            type: 3
        },
        {
            title: "标题百度中匹配",
            key: "is_match",
            value: "",
            type: 1
        },
        {
            title: "发布Domain",
            key: "fb_domain",
            value: "",
            type: 1
        },
        {
            title: "时间",
            key: "time",
            value: "",
            type: 2
        }
    ];
    // es的几种搜索方式
    const esSearchFieldOptions: SelectOption = [
        {
            label: "请选择",
            value: ""
        },
        {
            label: "多词查询",
            value: "match"
        },
        {
            label: "精准单值查询",
            value: "term"
        },
        {
            label: "精准多值查询",
            value: "terms"
        },
        {
            label: "包含查询",
            value: "match_phrase"
        },
        {
            label: "通配符查询",
            value: "wildcard"
        },
        {
            label: "字段查询",
            value: "exists"
        }
    ];
    // 位置Options
    const positionOptions: SelectOption = [
        {
            label: "头部",
            value: "top"
        },
        {
            label: "中部",
            value: "center"
        },
        {
            label: "尾部",
            value: "footer"
        }
    ];

    // 自定义属性
    const customAttrOptions: SelectOption = [
        { label: "头条", value: "h" },
        { label: "推荐", value: "c" },
        { label: "幻灯", value: "f" },
        { label: "特荐", value: "a" },
        { label: "滚动", value: "s" },
        { label: "加粗", value: "b" },
        { label: "图片", value: "p" },
        { label: "跳转", value: "j" },
        { label: "随机", value: "random" }
    ];

    const ruleStatusOptions: SelectOption = [
        { label: "是", value: 1 },
        { label: "否", value: 0 }
    ];
    const ruleStatusOptions2: SelectOption = [
        { label: "开启", value: 1 },
        { label: "关闭", value: 0 }
    ];
    const ruleStatusOptions3: SelectOption = [
        { label: "已完成", value: 1 },
        { label: "未完成", value: 0 }
    ];
    const targetKeywordOptions: SelectOption = [
        { label: "是", value: 1 },
        { label: "否", value: 0 }
    ];
    return {
        targetKeywordOptions,
        ruleStatusOptions,
        customAttrOptions,
        positionOptions,
        esSearchFieldOptions,
        filterOption,
        otherSearchOptions,
        esHostOptions,
        ruleStatusOptions2,
        ruleStatusOptions3,
        weekOptions
    };
}
export type { OtherSearchItem };
